package com.nicolaspigelet.dopplrclient.UI 
{
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	/**
	 * @author Nicolas Pigelet aka tanaki
	 */
	public class LoginUI extends Sprite
	{
		private var __container:Sprite;
		private var __coolvetica:Font;
		private var pseudo:TextField;
		
		public function LoginUI() 
		{
			__coolvetica = new Coolvetica();
			
			__container = new Sprite();
			__container.graphics.beginFill(0xffffff);
			__container.graphics.drawRect(0, 0, 300, 200);
			__container.filters = [new DropShadowFilter(2, 45, 0, .4, 6, 6, 1, 3)];
			
			var logo : Logo = new Logo();
			logo.x = (__container.width - logo.width) >> 1;
			logo.y = 10;
			__container.addChild(logo);
			
			var text : TextField = new TextField();
			text.embedFonts = true;
			text.multiline = true;
			text.autoSize = TextFieldAutoSize.CENTER;
			text.defaultTextFormat = new TextFormat(__coolvetica.fontName, 18, 0x000000, false, false, false, null, null, TextFormatAlign.CENTER);
			text.htmlText = "Bienvenue, pour commencer,<br/>entrez votre pseudo";
			text.y = logo.y + logo.height + 20;
			text.x = (__container.width - text.width) >> 1;
			__container.addChild(text);
			
			pseudo = new TextField();
			pseudo.type = TextFieldType.INPUT;
			pseudo.height = 20;
			pseudo.width = 100;
			pseudo.y = text.y + text.height + 20;
			pseudo.x = ((__container.width - pseudo.width) >> 1) - 25;
			pseudo.border = true;
			pseudo.borderColor = 0x000000;
			pseudo.defaultTextFormat = new TextFormat("Arial", 14, 0x000000);
			__container.addChild(pseudo);
			
			var btn : Sprite = new Sprite();
			btn.graphics.beginFill(0x000000);
			btn.graphics.drawRect( 0, -10, 40, 20);
			btn.x = pseudo.x + pseudo.width + 5;
			btn.y = pseudo.y + 10;
			btn.mouseChildren = false;
			btn.buttonMode = true;
			
			var text_btn : TextField = new TextField();
			text_btn.embedFonts = true;
			text_btn.autoSize = TextFieldAutoSize.CENTER;
			text_btn.defaultTextFormat = new TextFormat(__coolvetica.fontName, 18, 0xffffff);
			text_btn.text = "OK";
			text_btn.x = 7;
			text_btn.y = -12;
			
			btn.addChild(text_btn);
			__container.addChild(btn);
			addChild(__container);
			
			btn.addEventListener(MouseEvent.CLICK, __clickHandler);
		}
		
		private function __clickHandler(e:MouseEvent):void 
		{
			if ( pseudo.text != "" ) {
				pseudo.borderColor = 0x000000;
				dispatchEvent(new DataEvent(Event.INIT, false, false, pseudo.text));
			}
			else 
				pseudo.borderColor = 0xff3333;
		}
		
	}

}